package com.hkts.naturalgas.utils;


import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameJsonParseUtil
 * @Description
 * @Author yck
 * @Date2021-01-03 10:36
 * @Version V1.0
 **/
public class JsonParseUtil {


   /* private static ObjectMapper mapper = new ObjectMapper();

    static
    {
        // 允许整数前导为0,eg:"01"形式
        mapper.configure(Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        mapper.configure(Feature.ALLOW_COMMENTS, true);
        mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }

    *//**
     *
     * @param object
     * @return
     *//*
    public static String obj2Json(Object object)
    {
        try
        {
            if (null == object)
            {
                return "";
            }
            // 如果object为空字符串，不处理则会返回两个双引号
            if (object instanceof String && StringUtils.isBlank(object.toString()))
            {
                return "";
            }
            return mapper.writeValueAsString(object);
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
        return "";
    }

    *//**
     * 可将json字符串转成任何复杂对象即对象里可含各种复杂类型:List、Map、Set、Object、Object[]
     *
     * @param json
     * @param clazz
     * @return
     *//*
    public static <T> T json2Obj(String json, Class<T> clazz)
    {
        try
        {
            // 设置时间解析格式
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return mapper.readValue(json, clazz);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    *//**
     * 把map转换成实体对象
     *
     * @param map
     * @param clazz
     * @return
     *//*
    public static <T> T map2Obj(Map<String, Object> map, Class<T> clazz)
    {
        return mapper.convertValue(map, clazz);
    }

    *//**
     * 将json字符串转成复制类型即对象里面含有对象或者List<T>情形
     *
     * @return 复制类型
     *//*
    public static <T> T json2ComplexObj(String json, Class<T> clazz)
    {
        T t = null;
        try
        {
            *//**
             * 转换成复杂类型用TypeReference
             *//*
            t = mapper.readValue(json, new TypeReference<T>()
            {
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return t;
    }

    *//**
     * 此方法适合复杂类型的json字符串 ,eg:{"id":0,"userId":null} 若形如["11","22"]的简单数组可直接转成List
     *
     * @param jsonArr json数组
     * @param clazz
     * @return
     *//*
    @SuppressWarnings("unchecked")
    public static <T> List<T> jsonArr2List(String jsonArr, Class<T> clazz)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            List<Map<String, Object>> map = mapper.readValue(jsonArr, List.class);
            for (Map<String, Object> entry : map)
            {
                T t = map2Obj(entry, clazz);
                list.add(t);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return list;
    }

    *//**
     * 适合简单json数组转List eg:List<String>,List<Integer>
     *
     * @param jsonArr ["111","222"],[1, 2]
     * @param clazz
     * @return
     *//*
    @SuppressWarnings("unchecked")
    public static <T> List<T> simpleJson2List(String jsonArr, Class<T> clazz)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            list = mapper.readValue(jsonArr, List.class);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return list;
    }

    *//**
     * 将json字符串转换成Map
     *
     * @param json
     * @return
     *//*
    @SuppressWarnings("unchecked")
    public static Map<String, Object> json2Map(String json)
    {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        try
        {
            map = mapper.readValue(json, Map.class);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return map;
    }*/

}
